import pymysql


db = pymysql.connect("localhost", "debian-sys-maint", "ctysTk7upM0cm2sV", "test")  # 数据库的接口是db

cursor = db.cursor()
# cursor.execute("DROP TABLE IF EXISTS Login")
sql = '''CREATE TABLE if not exists Login (User_id INT NOT NULL AUTO_INCREMENT
,User_name  CHAR(20) NOT NULL,User_password CHAR(20) NOT NULL,PRIMARY KEY(User_id))'''  # MYSQL命令语句 建表
cursor.execute(sql)
cursor.close()


def server_wtin(**kwargs):
    sql_select_name = "SELECT User_name from Login where User_name = %s"     # 从注册网页POST信息到数据库  查询是否正确再返回一个相应的成功信号

    sql_insert_user = "INSERT INTO Login(User_name,User_password) VALUES (%s,%s)"
    value_1 = (username, password)  # 从注册网页GET数据储存到数据库
    cursor = db.cursor()
    db.ping(reconnect=True)

    flag = cursor.execute(sql_select_name, username)
    if flag:
        return False

    cursor.execute(sql_insert_user, value_1)
    cursor.close()
    db.commit()
    return True


def server_rdout(username, password):
    sql_select_name = "SELECT User_name, User_password from Login where User_name = %s"

    # print(username, password)
    if not username:
        username = 'NULL'

    cursor = db.cursor()
    db.ping(reconnect=True)
    cursor.execute(sql_select_name, username)
    db.commit()
    data = cursor.fetchone()

    # print(data)
    cursor.close()

    if data is None:
        return False
    key = data[1]

    # print(key)

    if key == password:
        return True
    else:
        return False
